package drh.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.ScalarHandler;

import drh.bean.Evaluate;
import drh.dao.BaseDao;
import drh.dao.EvaluateDao;
import handler.EvaluateResultSetHandler;

public class EvaluateDaoImpl extends BaseDao implements EvaluateDao {

	@Override
	public Integer deleteEvaluteById(Integer id) {
		try {
			return query.update("delete from evaluate where id=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public Integer updateEvaluate(Evaluate info) {
			try {
				return query.update("update evaluate set userid=?,productid=?,content=?,time=?,isdelete=? where id=?",info.getUserid(),info.getProductid(),
						info.getContent(),info.getTime(),info.getIsdelete(),info.getId());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return -1;
	}

	@Override
	public Integer addEvaluate(Evaluate info) {
		try {
			return query.update("insert into evaluate(userid,productid,content,time,isdelete) values(?,?,?,?,?)",info.getUserid(),
					info.getProductid(),info.getContent(),info.getTime(),info.getIsdelete());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<Evaluate> GetEvaluatePaging(Integer pageIndex, Integer pageSize) {
		String sql = "select e.* from evaluate e limit ?,?";
		try {
			return query.query(sql, new EvaluateResultSetHandler(),(pageIndex-1)*pageSize,pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Integer getCount() {
			String sql = "select count(1) from evaluate";
			Object query2 ;
			try {
				query2 = query.query(sql, new ScalarHandler());
				return Integer.parseInt(query2.toString());
			} catch (SQLException e) {
				e.printStackTrace();
			}			
		return null;
	}
	
}
